package o2o.web.actions.order.thread;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderInfoMemory;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.PO.order.ORDER_TYPE;
import o2o.dao.PO.order.OrderInfo;
import o2o.dao.PO.wallet.PAY_TYPE;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.order.OrderMan;
import o2o.web.actions.pay.OrderPay;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//订单  检测超时未支付订单  直接取消
public class OrderThread extends Thread {
    public static Map<String, OrderInfo> map = new ConcurrentHashMap<>();   //检测订单一定时间内未支付  直接取消

    private boolean threadFlag = true;

    private static OrderThread thread = new OrderThread();

    public static OrderThread getInstance() {
        if (thread == null) {
            thread = new OrderThread();
        }
        return thread;
    }

    private OrderThread() {
    }


    @Override
    public void run() {
        while (threadFlag) {
            try {
                Thread.sleep(120 * 1000);  //线程每次停止120秒  zhuwx
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Integer minutes = (SYS_SETTINGS.NOT_PAY_AUTO_CANCEL_ORDER_TIME > 0 ? SYS_SETTINGS.NOT_PAY_AUTO_CANCEL_ORDER_TIME : 30);

            if (map.size() > 0) {
                synchronized (map) {
                    //检测是否已支付成功
                    for (Map.Entry<String, OrderInfo> data : map.entrySet()) {
                        String key = data.getKey();
                        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(key);
                        if (null == orderInfo) {
                            map.remove(key);
                            continue;
                        }

                        // 货到付款的/线下付款的直接不需要查询
                        if (0 != orderInfo.getOrderStatus() - ORDER_STATUS.SUBMIT || (ValidateUtil.isNotNull(orderInfo.getPayType()) && (PAY_TYPE.UNDERLINE.equals(orderInfo.getPayType()) || PAY_TYPE.ARRIVE.equals(orderInfo.getPayType())))) {
                            map.remove(key);
                            continue;
                        }

                        //开锁订单不自动结束
                        if (null != orderInfo.getOrderType() &&
                                (0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()//开锁订单
                                        || 0 == ORDER_TYPE.SHOP_GOODS_PRE - orderInfo.getOrderType()//订货预订单
                                        || 0 == ORDER_TYPE.SHOP_GOODS_STORE - orderInfo.getOrderType()//订货单
                                )) {
                            map.remove(key);
                            continue;
                        }

                        //已经支付成功的就不惜要查了
                        if (ValidateUtil.isAbsInteger(orderInfo.getIsPaid()) && 0 == 1 - orderInfo.getIsPaid()) {
                            map.remove(key);
                            continue;
                        }

//                        if (DateUtils.getCurrTime() - orderInfo.getCreateTime() > 30 * 60 * 1000) {
//                            //订单创建时间时间错过30分钟才去检测
//                        } else {
//                            continue;
//                        }
                        if (DateUtils.getCurrTime() - orderInfo.getCreateTime() > minutes * 60 * 1000) {
                            //订单创建时间时间错过30分钟才去检测
                        } else {
                            continue;
                        }


                        //支付时间超过30分钟还未支付  直接取消订单
//                        if ((ValidateUtil.isAbsLong(orderInfo.getPayTime()) && DateUtils.getCurrTime() - orderInfo.getPayTime() > 30 * 60 * 1000)
                        if ((ValidateUtil.isAbsLong(orderInfo.getPayTime()) && DateUtils.getCurrTime() - orderInfo.getPayTime() > minutes * 60 * 1000)
                                || ValidateUtil.isNotAbsLong(orderInfo.getPayTime())) {
//                            //取消订单并删除
//                            Order.cancelOrder(map.get(key), "admin", "超时未支付", ORDER_STATUS.CANCEL_BY_SYSTEM);
//
//                            map.remove(key);
//

                            if (ValidateUtil.isNull(orderInfo.getPayType()) || ValidateUtil.isNotAbsLong(orderInfo.getPayTime())) {//未支付
                                //取消订单并删除
                                OrderMan.cancelOrderDirect(orderInfo, "admin", "超时未支付", ORDER_STATUS.CANCEL_BY_SYSTEM, false, true);
                                map.remove(key);
                                continue;
                            }

                            //查询一遍是否支付成功
                            ResultInfo resultInfo = OrderPay.userOrderPayOver(orderInfo.getSysAppKey(), orderInfo.getPayTypeFlag(),
                                    orderInfo.getPayType(), key, "");
                            if (null != resultInfo && resultInfo.isSuccess()) {
                                //已支付成功并且以流转
                                map.remove(key);
                            } else {
                                //未支付成功或有其他错误
                                //取消订单并删除
                                OrderMan.cancelOrderDirect(orderInfo, "admin", "超时未支付", ORDER_STATUS.CANCEL_BY_SYSTEM, false, true);
                                map.remove(key);
                            }
                        }
                    }
                }
            }
        }
    }

}
